﻿// Write a program that reads a positive integer number N (N < 20) from console and outputs in the console the numbers 1 ... N 
// numbers arranged as a spiral.
// Example for N = 4
// 1   2   3   4
// 12  13  14  5
// 11  16  15  6
// 10  9   8   7

using System;

class NumbersAsSpiral
{
    static void Main()
    {

        Console.Write("Enter N bigger than 0 and less than 20 : ");
        string numberStr = Console.ReadLine();
        int numberN;
        while (!int.TryParse(numberStr, out numberN))
        {
            Console.Write("Inavlid input. Try enter N bigger than 0 and less than 20 : ");
            numberStr = Console.ReadLine();
        }

        if (numberN <= 0 || numberN >= 20)
        {
            Console.WriteLine("Invalid input. N must be bigger than 0 and less than 20.");
        }
        else
        {
            int[,] spiral = new int[numberN, numberN];

            string direction = "right";

            int currentRow = 0;
            int currentCol = 0;

            for (int i = 1; i <= numberN * numberN; i++)
            {
                if (direction == "right" && (currentCol >= numberN || spiral[currentRow, currentCol] != 0))
                {
                    currentCol--;
                    currentRow++;
                    direction = "down";
                }
                else if (direction == "down" && (currentRow >= numberN || spiral[currentRow, currentCol] != 0))
                {
                    currentRow--;
                    currentCol--;
                    direction = "left";
                }
                else if (direction == "left" && (currentCol < 0 || spiral[currentRow, currentCol] != 0))
                {
                    currentCol++;
                    currentRow--;
                    direction = "up";
                }
                else if (direction == "up" && (currentRow < 0 || spiral[currentRow, currentCol] != 0))
                {
                    currentRow++;
                    currentCol++;
                    direction = "right";
                }

                spiral[currentRow, currentCol] = i;

                if (direction == "right")
                {
                    currentCol++;
                }
                else if (direction == "down")
                {
                    currentRow++;
                }
                else if (direction == "left")
                {
                    currentCol--;
                }
                else if (direction == "up")
                {
                    currentRow--;
                }
            }

            //print the spiral
            for (int i = 0; i < numberN; i++)
            {
                for (int j = 0; j < numberN; j++)
                {
                    Console.Write("{0, -5}", spiral[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
}
